package com.tingyun.sign.core.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.tingyun.sign.core.entity.ScActivityBean;
import com.tingyun.sign.core.entity.ScQuestionsBean;

@Repository("scQuestionsDao")
public interface ScQuestionsDao extends PagingAndSortingRepository<ScQuestionsBean, Integer>, JpaSpecificationExecutor<ScQuestionsBean> {

	/**
	 * select all the question asked during this activity
	 * @param activityId
	 * @return
	 */
	@Query(value = "select t from ScQuestionsBean t where t.activityId = :activityId order by ctime desc")
	public List<ScQuestionsBean> findByActivityId(@Param("activityId") Long activityId);

	@Query(value = "select t from ScQuestionsBean t where t.id = :id")
	public ScQuestionsBean findById(@Param("id") Long id);

	@Modifying
	@Query(value = "update ScQuestionsBean t set t.answer = :answer ,t.mtime = :mtime, t.muser = :muser where t.id = :id")
	public void updateAnswerById(@Param("id") Long id, @Param("answer") String answer, @Param("mtime") Date mtime, @Param("muser") Long muser);
	
	@Query(value = "select t from ScActivityBean t where t.status=0 and t.area = :managerType order by id desc")
	public List<ScActivityBean> findActivitiesByArea(@Param("managerType") int managerType);
	
	@Query(value = "select count(t.id) from ScQuestionsBean t where t.ctime >=:start and t.ctime <:end and t.activityId = :activityId")
	public Integer countQuestions(@Param("activityId")Long activityId,@Param("start")Date start, @Param("end")Date end);
	
	@Query(value = "select count(t.id) from ScQuestionsBean t where t.activityId = :activityId")
	public Integer countListQuestions(@Param("activityId")Long activityId);
}
